/*******************************************************************************
 *
 * File Name:          MessageResourcesActivity.java
 *
 * Created:            2006-10-19 21:05:35
 *
 * Original Author:    yu.peng
 *
 *------------------------------------------------------------------------------
 * MODIFICATION HISTORY
 *------------------------------------------------------------------------------
 * WHEN                WHO                 WHY
 * 2006-10-19 21:05:35 yu.peng             new
 *------------------------------------------------------------------------------
 *
 ******************************************************************************/
/*******************/
/* PACKAGE SECTION */
/*******************/

package org.njo.webapp.root.model.activity;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.njo.webapp.root.model.dao.MessageResourcesDAO;
import org.njo.webapp.root.utility.ObjectExistsException;
import org.njo.webapp.root.utility.ObjectNotExistsException;
import org.njo.webapp.root.utility.SystemContext;

/**
 * TODO:comment
 * 
 * @author yu.peng
 * @version 0.01
 */
public class MessageResourcesActivity {

    /**
     * Log Object.
     */
    private static Log logger = LogFactory
            .getLog(MessageResourcesActivity.class);

    public List getMessageResourcesList() {
        Connection connection = null;
        MessageResourcesDAO messageResourcesDAO = null;

        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            messageResourcesDAO = new MessageResourcesDAO(connection);
            return messageResourcesDAO.selectAllRecords(1, Integer.MAX_VALUE);
        } catch (SQLException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }
    }

    public void addMessage(String argMessageKey, String argDescription,
            Map argMessageContents) throws ObjectExistsException {

        Connection connection = null;
        MessageResourcesDAO messageResourcesDAO = null;

        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            messageResourcesDAO = new MessageResourcesDAO(connection);
            messageResourcesDAO.insertMessageResources(argMessageKey,
                    argDescription);

            Iterator entryIterator = argMessageContents.entrySet().iterator();
            while (entryIterator.hasNext()) {
                // Map.Entry
                Map.Entry entry = (Map.Entry) entryIterator.next();
                String messageLocaleKey = (String) entry.getKey();
                String messageContent = (String) entry.getValue();
                messageResourcesDAO.insertMessageResourcesI18N(argMessageKey,
                        messageLocaleKey, messageContent);
            }

            connection.commit();
        } catch (SQLException ex) {
            // when fail rollback the updated's data.
            try {
                connection.rollback();
            } catch (Throwable tw) {
                // when rollback fail,log the info.
                if (logger.isErrorEnabled()) {
                    logger.error(tw);
                }
            }
            if (ex.getErrorCode() == 1) { // ORACLE 主键重复
                throw new ObjectExistsException("aready inserted.");
            }
            if (ex.getErrorCode() == 1062) { // MySQL 主键重复
                throw new ObjectExistsException("aready inserted.");
            }
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }

    }

    public void saveMessage(String argMessageKey, String argDescription,
            Map argMessageContents) {

        Connection connection = null;
        MessageResourcesDAO messageResourcesDAO = null;

        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            messageResourcesDAO = new MessageResourcesDAO(connection);
            messageResourcesDAO.updateMessageResources(argMessageKey, argDescription);

            messageResourcesDAO.deleteMessageResourcesI18NByMessageKey(argMessageKey);

            Iterator entryIterator = argMessageContents.entrySet().iterator();
            while (entryIterator.hasNext()) {
                // Map.Entry
                Map.Entry entry = (Map.Entry) entryIterator.next();
                String messageLocaleKey = (String) entry.getKey();
                String messageContent = (String) entry.getValue();
                messageResourcesDAO.insertMessageResourcesI18N(argMessageKey,
                        messageLocaleKey, messageContent);
            }

            connection.commit();
        } catch (SQLException ex) {
            // when fail rollback the updated's data.
            try {
                connection.rollback();
            } catch (Throwable tw) {
                // when rollback fail,log the info.
                if (logger.isErrorEnabled()) {
                    logger.error(tw);
                }
            }
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }

    }

    public void removeMessage(String[] argMessageKeys) {
        Connection connection = null;
        MessageResourcesDAO messageResourcesDAO = null;

        try {
        } catch (Throwable tw) {
            logger.error(tw);
            throw new RuntimeException(tw);
        }

        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            messageResourcesDAO = new MessageResourcesDAO(connection);

            for (int i = 0;argMessageKeys!=null && i < argMessageKeys.length; i++) {
                String messageKey = argMessageKeys[i];
                messageResourcesDAO.deleteMessageResourcesI18NByMessageKey(messageKey);
                messageResourcesDAO.deleteMessageResourcesByMessageKey(messageKey);
            }
            
            connection.commit();
        } catch (SQLException ex) {
            // when fail rollback the updated's data.
            try {
                connection.rollback();
            } catch (Throwable tw) {
                // when rollback fail,log the info.
                if (logger.isErrorEnabled()) {
                    logger.error(tw);                    
                }
            }
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }
    }

    
    /**
     * 
     * 
     * @param argMessageKey
     * @return
     * @throws ObjectNotExistsException
     */
    public Map getMessageResources(String argMessageKey)
            throws ObjectNotExistsException {
        // selectByMessageKey
        Connection connection = null;
        MessageResourcesDAO messageResourcesDAO = null;
        Map messageResourcesMap = null;

        // access the database.
        try {
            // open the database connection.
            connection = SystemContext.openConnection();
            messageResourcesMap = new HashMap();
            messageResourcesDAO = new MessageResourcesDAO(connection);
            String[] messageResources = messageResourcesDAO
                    .selectMessageResourcesByMessageKey(argMessageKey);

            messageResourcesMap.put("messageKey", messageResources[0]);
            messageResourcesMap.put("description", messageResources[1]);

            List messageResourcesI18NList = messageResourcesDAO
                    .selectMessageResourcesI18NByMessageKey(argMessageKey);
            Map messageContents = new HashMap();
            for (int i = 0; i < messageResourcesI18NList.size(); i++) {
                String[] messageContent = (String[]) messageResourcesI18NList
                        .get(i);
                messageContents.put(messageContent[0], messageContent[1]);
            }
            messageResourcesMap.put("messageContents", messageContents);

            return messageResourcesMap;
        } catch (SQLException ex) {
            if (logger.isErrorEnabled()) {
                logger.error(ex);
            }
            throw new RuntimeException(ex);
        } finally {
            try {
                if (connection != null) {
                    SystemContext.closeConnection(connection);
                }
            } catch (Throwable tw) {
            }
        }
    }

}
